package com.VolunteerPlatform.Service.VolunteerService.Impl;

import com.VolunteerPlatform.Dao.BankInfoDao;
import com.VolunteerPlatform.Dao.VolunteerInfoDao;
import com.VolunteerPlatform.Entity.BankInfo;
import com.VolunteerPlatform.Entity.VolunteerInfo;
import com.VolunteerPlatform.Service.UtilityService.ExportDataExcel;
import com.VolunteerPlatform.Service.VolunteerService.BankInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DoveXL on 2018/12/7.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BankInfoServiceImpl implements BankInfoService{
    @Autowired
    private BankInfoDao bankInfoDao;

    @Autowired
    private VolunteerInfoDao volunteerInfoDao;

    public List<BankInfo> getBankInfoService(String Unit, String IDCardNumber, String Name, String BankAccount,String BankBranch,int Deleted,int Page, int PageList)
    {
        return bankInfoDao.getAllBankInfo(IDCardNumber,Name,BankAccount,BankBranch,Unit,Deleted,Page,PageList);
    }


    @Override
    public int updateBankInfoService(String IDCardNumber, String Name, String BankAccount, String BankBranch) {
        return bankInfoDao.updateBankInfo(IDCardNumber,Name,BankAccount,BankBranch);
    }


    public int ExportTeamInfoService(String Name,String IDCardNumber,String Unit,String title,String[] rowName,List<String> checkFieldNameList,String filename) throws IOException
    {
        List<BankInfo> BankInfoList = bankInfoDao.getAllBankInfo(IDCardNumber,Name,null,null,Unit,0,-1,-1);
        List<VolunteerInfo> volunteerInfos = volunteerInfoDao.getVolunteerInfo(IDCardNumber,Name,null,Unit,null,0,-1,-1);
        List<Object []> StringList = new ArrayList<>();
        int BankInfoSize = BankInfoList.size();
        int volunteerInfoSize = volunteerInfos.size();
        Map<String,String> map1 = new HashMap<>();
        for(int j=0;j<volunteerInfoSize;j++)
        {
            map1.put(volunteerInfos.get(j).getIDCardNumber(),volunteerInfos.get(j).getUnit());
        }
        for(int i=0;i<BankInfoSize;i++)
        {
            Map<String,String> BankInfoMap = new HashMap<>();
            BankInfo bankInfo = BankInfoList.get(i);
            BankInfoMap.put("Name",bankInfo.getName());
            BankInfoMap.put("Unit",map1.get(bankInfo.getIDCardNumber()));
            BankInfoMap.put("IDCardNumber",String.valueOf(bankInfo.getIDCardNumber()));
            BankInfoMap.put("BankAccount",bankInfo.getBankAccount());
            BankInfoMap.put("BankBranch",bankInfo.getBankBranch());
            String[] dataArray = new String[checkFieldNameList.size()];
            List<String> dataList = new ArrayList<>();
            for(int j = 0;j<checkFieldNameList.size();j++)
            {
                dataList.add(BankInfoMap.get(checkFieldNameList.get(j)));
            }
            dataList.toArray(dataArray);
            StringList.add(dataArray);
        }
        ExportDataExcel exportDataExcel = new ExportDataExcel(title,rowName,StringList,filename);
        try {
            exportDataExcel.ExportExcel();
            return 1;
        }catch (Exception e)
        {
            e.printStackTrace();
            throw  new IOException();
        }
    }
}
